package com.example.tourstor;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MyPlanActivity3 extends AppCompatActivity {
    private static final String TAG = "MyPlanActivity3";
    private TextView title,Tdestin,Tstart,Tcost,Smoney,wmt;
    private Button stop,pay,withdraw,title_btn_left,title_btn_right;
    private Thread t,t2;
    private Connection con;
    private PreparedStatement ps;
    private ResultSet rs=null;
    DBOpenHelper dbOpenHelper;


    private String tstart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_plan3);
        setTitle();
        init();

    }
    public void init(){
        Tdestin=findViewById(R.id.Tdestin);
        Tstart=findViewById(R.id.Tstart);
        Tcost=findViewById(R.id.Tcost);
        Smoney=findViewById(R.id.Smoney);
        wmt=findViewById(R.id.wmt);
        stop=findViewById(R.id.stop);
        pay=findViewById(R.id.pay);
        withdraw=findViewById(R.id.withdraw);


        Intent intent=getIntent();
        tstart=intent.getStringExtra("Tstart");

        if(tstart!=null){

            Tstart.setText(tstart);

            t=new Thread(new Runnable() {
                @Override
                public void run() {
                    con=dbOpenHelper.getConn();

                    String sql="select * from makeplan where Tstart='"+tstart+"'";
                    try {
                        ps= (PreparedStatement) con.prepareStatement(sql);
                        if(ps!=null){
                            rs= ps.executeQuery();
                            if(rs!=null){
                                while(rs.next()){

                                    Tdestin.setText(rs.getString("Tdestin"));
                                    Tcost.setText(String.valueOf(rs.getFloat("Tcost")));
                                    Smoney.setText(String.valueOf(rs.getFloat("Smoney")));

                                    String wmt1=rs.getString("Wtime");
                                    String  wmt2=rs.getString("Mtime");

                                    if(wmt1!=null){
                                        wmt.setText(wmt1);
                                    }else if(wmt2!=null){
                                        wmt.setText(wmt2);
                                    }

                                }
                            }
                        }


                    } catch (SQLException e) {
                        e.printStackTrace();
                    }



                }
            });
            t.start();


            stop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    t2=new Thread(new Runnable() {
                        @Override
                        public void run() {
                            con=dbOpenHelper.getConn();
                            java.sql.Date tstart1= java.sql.Date.valueOf(tstart);
                            String sql="delete from makeplan where datediff ( Tstart , '"+tstart1+"' ) = 0";
                            try {
                                int result=-1;
                                if(con!=null){
                                    ps= (PreparedStatement) con.prepareStatement(sql);
                                    result=ps.executeUpdate();//返回1 执行成功
                                    Log.i(TAG, "run: 返回result="+result);
                                }

                                con.close();//一定要关闭
                                ps.close();
                                rs.close();
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }

                        }
                    });
                    t2.start();
                }
            });


            withdraw.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(MyPlanActivity3.this,WithDrawActivity.class);
                    intent.putExtra("Smoney",Smoney.getText().toString());
                    intent.putExtra("Tstart",tstart);
                    startActivity(intent);
                }
            });


        }



    }
    public void setTitle(){
        title = findViewById(R.id.title_text);
        title.setText("我的计划");
        title_btn_left = findViewById(R.id.title_btn_left);
        title_btn_right=findViewById(R.id.title_btn_right);
        title_btn_left.setText("");
        title_btn_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title_btn_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
